package at.fhj.ase.presentation;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

import javax.xml.stream.XMLStreamException;

import at.fhj.ase.business.AttackDetector;
import at.fhj.ase.business.FilterParser;
import at.fhj.ase.business.OutputGenerator;
import at.fhj.ase.data.AttackOutput;

/**
 * 
 * @author Zandmiralvand, Mansouri, Jaklitsch
 * 
 * this class processes the user input
 * according to the user input it will call the appropriate business logic * 
 *
 */

public class InputHandler {
	
	private FilterParser parser;	
	private AttackDetector detector;
	private List<AttackOutput> detectedAttacks;
	
	public InputHandler() {
		parser = new FilterParser();
	}
	
	public static String takeInput() {
		String userInput = null;
		try
		{
			BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));			
			userInput = reader.readLine();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return userInput;
	}
	
	public void handleInput(String cmdString) throws IllegalArgumentException {
		
		int commandNumber = 0;
		try {
			commandNumber = Integer.valueOf(cmdString);
		} catch (NumberFormatException nfe) {
			throw new IllegalArgumentException("invalid menu choice");
		}
		//check if input is valid
		for (Command cmd : Command.values()) {
			if (commandNumber == cmd.commandNumber) {
				
				if (cmd.equals(Command.EXIT)) {
					//TODO: replace with a cleaner exit mechanism
					AIIStartup.continueExecution = Boolean.FALSE;
					System.out.println("Exiting the application ...\n\n");
				}
				else if (cmd.equals(Command.CHECK_ATTACKS)) {
					
					System.out.println("Performing intrusion check ...\n\n");
					
					try {
						
						detectAttacks();
						
					} catch (FileNotFoundException e) {
						e.printStackTrace();
					} catch (XMLStreamException e) {
						e.printStackTrace();
					} catch (IOException e) {
						e.printStackTrace();
					}
					
				}
				else if (cmd.equals(Command.VIEW_REPORT)) {
					
					System.out.println("Please specify the output file for the report:\n\n");
					String outputFileName = takeInput();
					
					if(detectedAttacks == null){
						try {
							detectAttacks();
							
						} catch (FileNotFoundException e) {
							e.printStackTrace();
						} catch (IOException e) {
							e.printStackTrace();
						} catch (XMLStreamException e) {
							e.printStackTrace();
						}
					}
					try {

						OutputGenerator.reportToXHTML(detectedAttacks, outputFileName);

					} catch (XMLStreamException e) {
						e.printStackTrace();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}				
		}		
	}
	private void detectAttacks() throws IOException, FileNotFoundException, XMLStreamException {
		detector = new AttackDetector(parser.extractFilters(parser.createFilterFileParser()));
		System.out.println("Please specify the path to the access log file:\n");
		String accessLogName = takeInput();
		detectedAttacks = detector.readAccessLogFile(accessLogName);
	}

}
